package com.cneport.das.server.trans.parse;


import com.cneport.das.common.model.trans.AddInfos;
import com.cneport.das.common.model.trans.ClientPutRequest;
import com.cneport.das.common.model.trans.ReceiverIds;
import com.cneport.das.common.model.trans.dxp.DxpMsg;
import com.cneport.das.common.model.trans.dxp.TransInfo;
import com.cneport.das.common.utils.DateUtil;
import com.cneport.das.common.utils.JAXBUtils;
import com.cneport.das.common.utils.StringUtil;
import com.cneport.das.server.trans.handler.BaseTransformer;
import com.cneport.das.server.trans.handler.TransformerFamily;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;

/**
 * @author GavinZhang
 * @version 1.0
 * @desc： 对请求报文进行解析，并组装成上行DXP报文
 * @company 东方物通科技有限（北京）公司
 * @date 2021/8/27 8:30
 */
@Component
@Slf4j
public class ParserAndWrapper {


    private static final String VER = "1.0";

    /**
     * 将 ClientPutRequest 请求的报文解密后转换成DXP的XML格式的字符串
     *
     * @param req
     * @return
     */
    public String wrapperPut(ClientPutRequest req) {
        //请求信息
        ClientPutRequest.ReqHead reqHead = req.getReqHead();
        ClientPutRequest.ReqData data = req.getReqData();
        //客户指定的请求参数转化器
        String trans = data.getTrans();
        if (!StringUtil.isBlank(trans)) {
            String bzdata = data.getData();
            Map<String, BaseTransformer> map = TransformerFamily.getAllTransformer();
            BaseTransformer trs = map.get(trans);
            if (trs != null) {
                //业务报文的格式转换
                bzdata = trs.handle(bzdata);
                data.setData(bzdata);
            }
        }

        List<AddInfos.AddInfo> addInfos = req.getAddInfos().getAddInfo();
        String addinfo = "";
        StringBuilder sb = new StringBuilder();
        if (addInfos != null && addInfos.size() > 0) {
            sb.append("{");
            for (AddInfos.AddInfo info : addInfos) {
                String name = info.getName();
                if (StringUtil.isBlank(name)) {
                    continue;
                }
                String value = StringUtil.isBlank(info.getValue()) ? "" : info.getValue();
                sb.append("\"" + name + "\":\"" + value + "\",");

            }
            String str = sb.toString();
            str = str.substring(0, str.length() - 1);
            str = str + "}";
            addinfo = str;
            sb = null;
        }
        String ver = req.getReqHead().getVer();

        //DXP报文
        DxpMsg dxpMsg = new DxpMsg();

        TransInfo transInfo = new TransInfo();

        ReceiverIds receiverIds = reqHead.getReceiverIds();
        com.cneport.das.common.model.trans.dxp.ReceiverIds dxpReceiveIds = new com.cneport.das.common.model.trans.dxp.ReceiverIds();
        if (receiverIds != null) {
            dxpReceiveIds.setReceiverId(receiverIds.getReceiverId());
        }
        dxpMsg.setData(data.getData());

        transInfo.setCopMsgId(reqHead.getCopMsgId());
        transInfo.setCreatTime(DateUtil.C14());
        transInfo.setMsgType(reqHead.getMsgType());
        transInfo.setReceiverIds(dxpReceiveIds);
        transInfo.setSenderId(reqHead.getSenderId());

        dxpMsg.setTransInfo(transInfo);
        dxpMsg.setData(data.getData());
        dxpMsg.setVer(StringUtil.isBlank(ver) ? VER : ver);
        dxpMsg.setId(reqHead.getGuid());
        dxpMsg.setAddInfo(addinfo);

        String xml = JAXBUtils.obj2Xml(dxpMsg);

        return xml;
    }

}
